package com.woniuxy.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.sales.model.entity.Goods;
import com.woniuxy.sales.model.entity.Orders;
import com.woniuxy.sales.service.GoodsService;
import com.woniuxy.sales.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【goods】的数据库操作Service实现
* @createDate 2024-11-05 15:27:59
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;


    @Override //分页查询所有商品
    public Page<Goods> findAll(Integer pageNum, Integer pageSize) {
        //1.创建分页对象
        Page<Goods> goodsPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Goods::getStoreCount);//库存量小的排前面
        //3.执行查询
        Page<Goods> page = goodsMapper.selectPage(goodsPage, wrapper);
        //4.返回分页对象
        return page;
    }
    @Override //根据类别分页查询商品
    public Page<Goods> findByType(Integer pageNum, Integer pageSize, Integer type) {
        //1.创建分页对象
        Page<Goods> goodsPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        //如果不为0，则查询指定类别的商品
        if (type != 0) {
            wrapper.eq(Goods::getType,type);
        }
        wrapper.orderByAsc(Goods::getStoreCount);//库存量小的排前面
        //3.执行查询
        Page<Goods> page = goodsMapper.selectPage(goodsPage, wrapper);
        //4.返回分页对象
        return page;
    }



    @Override
    public Goods findById(Integer id) {
        return goodsMapper.selectById(id);
    }

}




